﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using VPLink2013.Framework;
using VPLink2013.Framework.Extensions;
using VPLink2014.Data;
using VPLink2014.Data.MesApplis;
using System.Linq;

namespace VPLink2014
{
    public partial class UserInfo : LayoutsPageBase
    {
        private User getCurrentDbUser()
        {
            string userName = Context.User.Identity.Name;
            User user = null;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (VPLinkLogConnection db = new VPLinkLogConnection())
                    {
                        user = db.VplinkEntities.GetUserByLoginName(userName);
                    }
                });
            }
            catch (Exception ex)
            {
                ContextTrace.AddTrace(string.Format("exception lors de la récupération du userinfo de la db"));
                ContextTrace.AddTrace(string.Format("ex : {0}", ex.Message));
                ContextTrace.AddTrace(string.Format("ex : {0}", ex.StackTrace));
            }
            return user;
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            string responseJson = "{}";
            try
            {
                User currentUser = getCurrentDbUser();
                if (currentUser != null)
                {
                    responseJson = currentUser.ToJson();
                }
                Response.Write(responseJson);
                Response.End();
            }
            catch (System.Threading.ThreadAbortException)
            {
            }
            catch (Exception ex)
            {
                responseJson = string.Format(@"{{""messageError"" : ""{0}"", ""stackTrace"" : ""{1}""}}", ex.Message.ToJson(), ex.StackTrace.ToJson());
                Response.Write(responseJson);
                Response.End();
            }
        }
    }
}
